package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.guesstv.lib.module.GtDataProvider;
import com.stark.guesstv.lib.module.GtOptionGenerator;
import com.stark.guesstv.lib.module.bean.TvActorBean;
import com.stark.guesstv.lib.module.constant.GuessType;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelTextAdapter;
import flc.ast.bean.MyTextBean;
import flc.ast.databinding.ActivityGame1Binding;
import flc.ast.utils.Content;
import stark.common.basic.utils.FastClickUtil;
import stark.common.basic.utils.SPUtil;

public class Game1Activity extends BaseAc<ActivityGame1Binding> {

    //看图猜电视剧
    public static int level = 0;
    private List<TvActorBean> listAll = new ArrayList<>();
    private SelTextAdapter textAdapter;
    private String rightAnswer = "";
    private String rightText1 = "";
    private String rightText2 = "";
    private String rightText3 = "";
    private String rightText4 = "";
    private List<MyTextBean> listShow = new ArrayList<>();
    private int nowPos = 0;
    private boolean isRight = false;
    private Dialog myRightDialog;
    private TextView dialogText;

    @Override
    protected int onCreate() {
        return R.layout.activity_game1;
    }

    @Override
    protected void initView() {
        mDataBinding.ivGame1Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mDataBinding.tvGame1Last.setOnClickListener(this);
        mDataBinding.tvGame1Next.setOnClickListener(this);
        mDataBinding.ivGame1Tip.setOnClickListener(this);
        mDataBinding.rvGame1TextList.setLayoutManager(new GridLayoutManager(mContext, 5));
        textAdapter = new SelTextAdapter();
        mDataBinding.rvGame1TextList.setAdapter(textAdapter);
        textAdapter.setOnItemClickListener(this);
        //正确弹框
        rightDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvGame1Last://上一关
                if (level == 0) {
                    ToastUtils.showShort(R.string.it_is_already_the_first_level);
                    return;
                }
                level--;
                getData();
                break;
            case R.id.tvGame1Next://下一关
                if (level == (listAll.size() - 1)) {
                    ToastUtils.showShort(R.string.it_is_already_the_last_level);
                    return;
                }
                int spLevel = SPUtil.getInt(mContext, Content.GAME_NAME1, 1);
                if (level == (spLevel - 1)) {
                    ToastUtils.showShort(R.string.please_pass_now_level);
                    return;
                }
                level++;
                getData();
                break;
            case R.id.ivGame1Tip://提示
                tipAnswer();
                break;
            //回答正确弹框
            case R.id.ivDialogRightBack://返回
                myRightDialog.dismiss();
                getData();
                break;
            case R.id.ivDialogRightNext://下一关
                myRightDialog.dismiss();
                if (level == (listAll.size() - 1)) {
                    ToastUtils.showShort(R.string.it_is_already_the_last_level);
                    getData();
                    return;
                }
                level++;
                getData();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (textAdapter.getItem(position).isSelect() || isRight) return;
        if (FastClickUtil.isFastClick()) return;
        textAdapter.getItem(position).setSelect(true);
        textAdapter.notifyItemChanged(position);
        switch (nowPos) {
            case 0:
                nowPos++;
                mDataBinding.tvGame1Answer1.setBackgroundResource(R.drawable.an_wz1);
                mDataBinding.tvGame1Answer2.setBackgroundResource(R.drawable.an_wz);
                mDataBinding.tvGame1Answer1.setText(textAdapter.getItem(position).getText());
                break;
            case 1:
                nowPos++;
                mDataBinding.tvGame1Answer2.setBackgroundResource(R.drawable.an_wz1);
                mDataBinding.tvGame1Answer3.setBackgroundResource(R.drawable.an_wz);
                mDataBinding.tvGame1Answer2.setText(textAdapter.getItem(position).getText());
                break;
            case 2:
                nowPos++;
                mDataBinding.tvGame1Answer3.setBackgroundResource(R.drawable.an_wz1);
                mDataBinding.tvGame1Answer4.setBackgroundResource(R.drawable.an_wz);
                mDataBinding.tvGame1Answer3.setText(textAdapter.getItem(position).getText());
                break;
            case 3:
                mDataBinding.tvGame1Answer4.setBackgroundResource(R.drawable.an_wz1);
                mDataBinding.tvGame1Answer4.setText(textAdapter.getItem(position).getText());
                isRight();
                break;
        }
    }

    @Override
    protected void initData() {
        listAll.clear();
        List<TvActorBean> tvData = GtDataProvider.getTvData();
        for (TvActorBean bean : tvData) {
            if (bean.name.length() == 4) {
                listAll.add(bean);
            }
        }
        getData();
    }

    private void getData() {
        String name = listAll.get(level).name;//答案
        List<String> list = GtOptionGenerator.generateOptions(GuessType.TV, listAll.get(level), 15);//选项集合
        mDataBinding.ivGame1Img.setImageBitmap(GtDataProvider.getGuessImgBitmap(listAll.get(level).imgName));//图片
        //设置关卡数据
        rightAnswer = name;
        String[] split = name.split("");
        rightText1 = split[0];
        rightText2 = split[1];
        rightText3 = split[2];
        rightText4 = split[3];
        listShow.clear();
        for (String text : list) {
            listShow.add(new MyTextBean(text, false));
        }
        textAdapter.setList(listShow);
        mDataBinding.tvGame1Level.setText(getString(R.string.level_left) + (level + 1) + getString(R.string.level_right));
        //恢复关卡状态
        nowPos = 0;
        mDataBinding.tvGame1Answer1.setText("");
        mDataBinding.tvGame1Answer2.setText("");
        mDataBinding.tvGame1Answer3.setText("");
        mDataBinding.tvGame1Answer4.setText("");
        mDataBinding.tvGame1Answer1.setBackgroundResource(R.drawable.an_wz);
        mDataBinding.tvGame1Answer2.setBackgroundResource(R.drawable.an_wz1);
        mDataBinding.tvGame1Answer3.setBackgroundResource(R.drawable.an_wz1);
        mDataBinding.tvGame1Answer4.setBackgroundResource(R.drawable.an_wz1);
    }

    //判断答案是否正确
    private void isRight() {
        isRight = true;
        String text1 = mDataBinding.tvGame1Answer1.getText().toString();
        String text2 = mDataBinding.tvGame1Answer2.getText().toString();
        String text3 = mDataBinding.tvGame1Answer3.getText().toString();
        String text4 = mDataBinding.tvGame1Answer4.getText().toString();
        String answer = text1 + text2 + text3 + text4;
        if (rightAnswer.equals(answer)) {
            //判断是否需要解锁下一关
            if (level < (listAll.size() - 1)) {//如果当前关卡小于总关卡数
                int spLevel1 = SPUtil.getInt(mContext, Content.GAME_NAME1, 1);//获取记录通过关卡数
                if (level == (spLevel1 - 1)) {//如果通过的是未解锁关卡则解锁下一关
                    SPUtil.putInt(mContext, Content.GAME_NAME1, spLevel1 + 1);
                }
            }
            isRight = false;
            dialogText.setText(getString(R.string.answer_text_left) + rightAnswer + "》");
            myRightDialog.show();
        } else {
            mDataBinding.ivGame1TipText.setVisibility(View.VISIBLE);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mDataBinding.ivGame1TipText.setVisibility(View.INVISIBLE);
                    isRight = false;
                    //恢复关卡状态
                    nowPos = 0;
                    mDataBinding.tvGame1Answer1.setText("");
                    mDataBinding.tvGame1Answer2.setText("");
                    mDataBinding.tvGame1Answer3.setText("");
                    mDataBinding.tvGame1Answer4.setText("");
                    mDataBinding.tvGame1Answer1.setBackgroundResource(R.drawable.an_wz);
                    mDataBinding.tvGame1Answer2.setBackgroundResource(R.drawable.an_wz1);
                    mDataBinding.tvGame1Answer3.setBackgroundResource(R.drawable.an_wz1);
                    mDataBinding.tvGame1Answer4.setBackgroundResource(R.drawable.an_wz1);
                    for (MyTextBean bean : textAdapter.getValidData()) {
                        bean.setSelect(false);
                    }
                    textAdapter.notifyDataSetChanged();
                }
            }, 1000);
        }
    }

    //提示
    private void tipAnswer() {
        String text1 = mDataBinding.tvGame1Answer1.getText().toString();
        String text2 = mDataBinding.tvGame1Answer2.getText().toString();
        String text3 = mDataBinding.tvGame1Answer3.getText().toString();
        String text4 = mDataBinding.tvGame1Answer4.getText().toString();
        switch (nowPos) {
            case 0:
                nowPos++;
                mDataBinding.tvGame1Answer1.setBackgroundResource(R.drawable.an_wz1);
                mDataBinding.tvGame1Answer2.setBackgroundResource(R.drawable.an_wz);
                mDataBinding.tvGame1Answer1.setText(rightText1);
                setPosition(text1, rightText1);
                break;
            case 1:
                if (!text1.equals(rightText1)) {
                    mDataBinding.tvGame1Answer1.setText(rightText1);
                    setPosition(text1, rightText1);
                    return;
                }
                nowPos++;
                mDataBinding.tvGame1Answer2.setBackgroundResource(R.drawable.an_wz1);
                mDataBinding.tvGame1Answer3.setBackgroundResource(R.drawable.an_wz);
                mDataBinding.tvGame1Answer2.setText(rightText2);
                setPosition(text2, rightText2);
                break;
            case 2:
                if (!text1.equals(rightText1)) {
                    mDataBinding.tvGame1Answer1.setText(rightText1);
                    setPosition(text1, rightText1);
                    return;
                }
                if (!text2.equals(rightText2)) {
                    mDataBinding.tvGame1Answer2.setText(rightText2);
                    setPosition(text2, rightText2);
                    return;
                }
                nowPos++;
                mDataBinding.tvGame1Answer3.setBackgroundResource(R.drawable.an_wz1);
                mDataBinding.tvGame1Answer4.setBackgroundResource(R.drawable.an_wz);
                mDataBinding.tvGame1Answer3.setText(rightText3);
                setPosition(text3, rightText3);
                break;
            case 3:
                if (!text1.equals(rightText1)) {
                    mDataBinding.tvGame1Answer1.setText(rightText1);
                    setPosition(text1, rightText1);
                    return;
                }
                if (!text2.equals(rightText2)) {
                    mDataBinding.tvGame1Answer2.setText(rightText2);
                    setPosition(text2, rightText2);
                    return;
                }
                if (!text3.equals(rightText3)) {
                    mDataBinding.tvGame1Answer3.setText(rightText3);
                    setPosition(text3, rightText3);
                    return;
                }
                mDataBinding.tvGame1Answer4.setBackgroundResource(R.drawable.an_wz1);
                mDataBinding.tvGame1Answer4.setText(rightText4);
                setPosition(text4, rightText4);
                isRight();
                break;
        }
    }

    //设置选项集合状态
    private void setPosition(String onText, String offText) {
        for (MyTextBean bean : textAdapter.getValidData()) {
            if (bean.getText().equals(onText)) {
                bean.setSelect(false);//恢复显示
                break;
            }
        }
        for (MyTextBean bean : textAdapter.getValidData()) {
            if (bean.getText().equals(offText)) {
                bean.setSelect(true);//选中隐藏
                break;
            }
        }
        textAdapter.notifyDataSetChanged();
    }

    //回答正确弹框
    private void rightDialog() {
        myRightDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_right, null);
        myRightDialog.setContentView(inflate);
        myRightDialog.setCancelable(true);
        //获取当前Activity所在的窗体
        Window dialogWindow = myRightDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView dialogBack = inflate.findViewById(R.id.ivDialogRightBack);
        ImageView dialogNext = inflate.findViewById(R.id.ivDialogRightNext);
        dialogText = inflate.findViewById(R.id.tvDialogRightText);
        dialogBack.setOnClickListener(this);
        dialogNext.setOnClickListener(this);
    }

}